// -----------------------------------------------------------------------
//  <copyright file="GetDictionaryTypeNodeQuery.cs" company="LiuliuSoft">
//      Copyright (c) 2025 66SOFT. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2025-10-17 01:10</last-date>
// -----------------------------------------------------------------------

using DaprPlus.Authorization;
using DaprPlus.DataDicts.Domain.Dtos;
using DaprPlus.DataDicts.Domain.Entities;
using DaprPlus.DataDicts.Domain.Specs;
using DaprPlus.MediatR;


namespace DaprPlus.DataDicts.Application.Queries;

[AuthConfig(typeof(DictionaryType), ResourceAccessType.RoleLimit, "GetNode")]
public class GetDictionaryTypeNodeQuery(string? keyword) : SingleQuery<DictionaryTypeNodeOutDto[]>
{
    public string? Keyword { get; set; } = keyword;
}

public class GetDictionaryTypeNodeQueryHandler(IServiceProvider provider) 
    : IRequestHandler<GetDictionaryTypeNodeQuery, DictionaryTypeNodeOutDto[]>
{
    public async Task<DictionaryTypeNodeOutDto[]> Handle(GetDictionaryTypeNodeQuery request, CancellationToken cancellationToken)
    {
        var logger = provider.GetLogger<GetDictionaryTypeNodeQueryHandler>();
        var queryRepository = provider.GetQueryRepository<DictionaryType>();
        var spec = new DictionaryTypeUnlockedSpec().And(new DictionaryTypeByKeywordSpec(request.Keyword));
        var nodes = await queryRepository.GetAllAsync<DictionaryTypeNodeOutDto>(spec, cancellationToken);
        logger.LogInformation("获取字典类型节点列表，关键字：{Keyword}，结果数：{Count}",
            request.Keyword, nodes.Count);
        return [.. nodes];
    }
}
